<template>
    <panel-head :route="route" />
    <el-table :data="tableData.list" style="width: 100%">
        <el-table-column prop="id" label="id" />
        <el-table-column prop="name" label="昵称" />
        <el-table-column prop="permissions_id" label="所属组别">
            <template #default="scope">
                {{ permissionName(scope.row.permission_id) }}
            </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" />
        <el-table-column prop="active" label="状态">
            <template #default="scope">
                <el-tag :type="scope.row.active ? 'success' : 'danger'">
                    {{ scope.row.active ? '成功' : '失败' }}
                </el-tag>
            </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
            <template #default="scope">
                <div class="flex_box">
                    <el-icon><Clock /></el-icon>
                    <span style="margin-left: 10px">{{ scope.row.create_time }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template #default="scope">
                <el-button type="primary" @click="open(scope.row)">编辑</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination-info">
        <el-pagination
            v-model:currentPage="paginationData.pageNum"
            :page-size="paginationData.pageSize"
            :size="small"
            :background="false"
            layout="total, prev, pager, next, jumper"
            :total="tableData.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
    </div>

    <!-- 弹窗 -->
    <el-dialog v-model="dialogFormVisible" :before-close="beforeClose" width="500">
        <el-form ref="formRef" :model="form" label-width="100px" label-position="left" :rules="rules">
            <el-form-item label="手机号" prop="mobile">
                <el-input v-model="form.mobile" disabled></el-input>
            </el-form-item>
            <el-form-item label="昵称" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="菜单权限" prop="permission_id">
                <el-select v-model="form.permission_id" placeholder="请选择菜单权限" style="width: 240px">
                    <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="confirm(formRef)">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { authAdmin, selectList, updateUser } from '@/api'
import { ref, reactive, onMounted } from 'vue'
import dayjs from 'dayjs'
import { Clock } from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'

const route = useRoute()
console.log('route', route)
const paginationData = reactive({
    pageNum: 1,
    pageSize: 10,
})

const handleSizeChange = (val) => {
    paginationData.pageSize = val
    getListData()
}
const handleCurrentChange = (val) => {
    paginationData.pageNum = val
    getListData()
}

// 列表数据
const tableData = reactive({
    list: [],
    total: 0,
})
const options = ref([])
onMounted(() => {
    selectList().then(({ data }) => {
        console.log('下拉列表', data)
        options.value = data.data
    })
    getListData()
})
// 请求列表
const getListData = () => {
    authAdmin(paginationData).then(({ data }) => {
        console.log(data)
        const { list, total } = data.data //解构出list、total
        list.forEach((item) => {
            //遍历整个数据list并转化格式
            item.create_time = dayjs(item.create_time).format('YYYY-MM-DD')
        })
        tableData.list = list
        tableData.total = total
    })
}

// 根据权限id匹配权限名称
const permissionName = (id) => {
    const data = options.value.find((el) => el.id === id)
    return data ? data.name : '超级管理员'
}
// 弹窗显示
const dialogFormVisible = ref(false)
const beforeClose = () => {
    dialogFormVisible.value = false
}
// 编辑表单
const formRef = ref()
const form = reactive({
    name: '',
    permission_id: '',
})
const open = (row) => {
    console.log('row', row)
    dialogFormVisible.value = true
    Object.assign(form, {
        mobile: row.mobile,
        name: row.name,
        permission_id: row.permission_id,
    })
}
const rules = reactive({
    name: [{ required: true, trigger: 'blur', message: '请填写昵称' }],
    permission_id: [{ required: true, trigger: 'blur', message: '请选择菜单权限' }],
})
// 表单提交
const confirm = async (foreEl) => {
    if (!foreEl) return
    await foreEl.validate((valid, fields) => {
        if (valid) {
            const params = {
                name: form.name,
                permission_id: form.permission_id,
            }
            updateUser(params).then(({ data }) => {
                console.log(data)
                if (data.code === 10000) {
                    dialogFormVisible.value = false
                    getListData()
                }
            })
        } else {
            console.log('error', fields)
        }
    })
}
</script>
<style lang="less" scoped>
.flex_box {
    display: flex;
    align-items: center;
}
</style>
